<?php

class Stats {

    protected $dbo = NULL;

    public function __construct() {

        $this->dbo  = &DBO::getInstance();
    }

    public function addVisit($ip, $referer, $page, $user_id) {

        $res = $this->dbo->query("INSERT INTO sys.visits
            (ip, referer, page, user_id)
            VALUES (?, ?, ?, ?)",
            array(
                DBString::create($ip),
                DBString::create($referer),
                DBString::create($page),
                DBInteger::create($user_id)
            ));

        throwOnError($res);

        return ($res == DB_OK);
    }

    public function getTotalArtists() {

        $res = $this->dbo->getOne('SELECT COUNT(*) FROM mtl.artists');

        throwOnError($res);

        return intval($res);
    }

    public function getTotalReleases() {

        $res = $this->dbo->getOne("SELECT COUNT(*) FROM mtl.releases");

        throwOnError($res);

        return intval($res);
    }

    public function getTotalUpcomingSchedules() {

        $res = $this->dbo->getOne("SELECT COUNT(*)
            FROM mtl.releases
            WHERE schedule_date IS NOT NULL
            AND mtl.is_schedule_date_ym(schedule_date)");

        throwOnError($res);

        return intval($res);
    }

    public function getTotalUsers() {

        $res = $this->dbo->getOne("SELECT COUNT(*) FROM sys.users");

        throwOnError($res);

        return intval($res);
    }
    
}

